package com.jz.community.moduleshoppingguide.home.ui.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.base.BaseX5WebActivity;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.net.retrofit.BaseRxObserver;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RoundCornersTransformation;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.home.bean.HomeFindBean;
import com.jz.community.moduleshoppingguide.home.bean.ShareBean;
import com.jz.community.moduleshoppingguide.home.model.ShoppingModelGuideImpl;
import com.jz.community.moduleshoppingguide.home.ui.activity.BestSellersActivity;
import com.jz.community.moduleshoppingguide.home.ui.activity.PreferentialActivity;
import com.jz.community.moduleshoppingguide.home.utils.HomeConst;
import com.jz.community.moduleshoppingguide.nearshop.ui.activity.NearShopActivity;
import com.jz.community.moduleshoppingguide.neighbor.ui.activity.NeighborMainActivity;

import java.util.List;


public class HomeMainActPinAdapter extends BaseQuickAdapter<HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean, BaseViewHolder> {

    public HomeMainActPinAdapter(int layoutResId, @Nullable List<HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean entity) {
        ImageView banner = helper.getView(R.id.home_main_pin_iv);
        RelativeLayout home_subject_layout = helper.getView(R.id.home_pin_subject_layout);
        TextView home_subject_tv = helper.getView(R.id.home_pin_subject_tv);
        RoundCornersTransformation transformation = new RoundCornersTransformation(mContext,
                SHelper.dp2px(mContext, 5), RoundCornersTransformation.CornerType.ALL);
        GlideApp.with(mContext)
                .load(entity.getImage())
                .transform(transformation)
                .placeholder(R.mipmap.default_img_2_1)
                .error(R.mipmap.default_img_2_1)
                .into(banner);
        banner.setOnClickListener(v -> {
            if (HomeConst.SPECIAL_URL.equals(entity.getJumpType())) {
                mContext.startActivity(new Intent(mContext, BaseX5WebActivity.class)
                        .putExtra("url", entity.getJumpParameter())
                        .putExtra("title", entity.getUrlTitle()));
            } else if (HomeConst.HOT_SALE.equals(entity.getJumpType())) {
                Intent intent = new Intent(mContext, BestSellersActivity.class);
                intent.putExtra("message", 1);
                intent.putExtra("name", "热卖推荐");
                mContext.startActivity(intent);
            } else if (HomeConst.COMMODITY_DETAILS.equals(entity.getJumpType())) {
                ARouter.getInstance()
                        .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                        .withString("url", entity.getGoodsLink())
                        .navigation();
            } else if (HomeConst.NEIGHBOR.equals(entity.getJumpType())) {
                Intent intent = new Intent(mContext, NeighborMainActivity.class);
                mContext.startActivity(intent);
            } else if (HomeConst.CARD_MORE.equals(entity.getJumpType())) {
                RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.CARD_HOME);
            } else if (HomeConst.PAY_THE_PHONE_BILL.equals(entity.getJumpType())) {
                RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.RECHARGE_PHONE);
            } else if (HomeConst.DISCOUNT_SPECIAL.equals(entity.getJumpType())) {
                Intent intent = new Intent(mContext, PreferentialActivity.class);
                mContext.startActivity(intent);
            } else if (HomeConst.INTEGRAL_SHOP.equals(entity.getJumpType())) {
                RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.INTEGRAL_GOODS);
            } else if (HomeConst.INVITATION_GIFT.equals(entity.getJumpType())) {
                new ShoppingModelGuideImpl(mContext).initShareGift(new BaseRxObserver<ShareBean>() {
                    @Override
                    public void onSuccess(ShareBean shareBean) {
                        mContext.startActivity(new Intent(mContext, BaseX5WebActivity.class)
                                .putExtra("url", shareBean.getData())
                                .putExtra("title", "分享有礼"));
                    }
                });
            } else if (HomeConst.NEARSHOP.equals(entity.getJumpType())) {
                Intent intent = new Intent(mContext, NearShopActivity.class);
                mContext.startActivity(intent);
            }
        });

        //标题
        if (!Preconditions.isNullOrEmpty(entity.getLabel())) {
            home_subject_layout.setVisibility(View.VISIBLE);
            home_subject_tv.setText(entity.getLabel());
        } else {
            home_subject_layout.setVisibility(View.GONE);
        }

        int image_width = (SHelper.getScrrenWidth((Activity) mContext) - SHelper.dp2px(mContext, 20));

        ViewGroup.LayoutParams lp1 = banner.getLayoutParams();
        lp1.width = image_width;
        lp1.height = image_width / 2;
        banner.setLayoutParams(lp1);

        RecyclerView pin_rv = helper.getView(R.id.home_main_pin_rv);

        if (null != entity.getActivityGoods() && entity.getActivityGoods().size() > 0) {
            pin_rv.setVisibility(View.VISIBLE);

            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);

            pin_rv.setHasFixedSize(true);
            pin_rv.setFocusable(false);
            pin_rv.setLayoutManager(layoutManager);

            final List<HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean.ActivityGoodsBean> goodsList = entity.getActivityGoods();

            final HomeMainActPinGoodsAdapter adapter_goods = new HomeMainActPinGoodsAdapter(R.layout.module_shoppingguide_item_home_act_two, goodsList);
            pin_rv.setAdapter(adapter_goods);

            adapter_goods.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    ARouter.getInstance()
                            .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                            .withString("url", goodsList.get(position).get_links())
                            .navigation();
                }
            });
        } else {
            pin_rv.setVisibility(View.GONE);
        }
    }
}
